package com.magiccompass.alipay.barcode.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.magiccompass.alipay.barcode.common.DicInit;
import com.magiccompass.alipay.barcode.entity.MerchantBankAccount;
import com.magiccompass.alipay.barcode.entity.MerchantGroup;
import com.magiccompass.alipay.barcode.entity.MerchantInfo;
import com.magiccompass.alipay.barcode.entity.UserInfo;
import com.magiccompass.alipay.barcode.pageform.MerchantGroupListPageForm;
import com.magiccompass.alipay.barcode.pageform.MerchantInfoListPageForm;
import com.magiccompass.alipay.barcode.pageform.MerchantOperatorListPageForm;
import com.magiccompass.alipay.barcode.pageform.UpdateMerchantInfoPageForm;

public class MerchantInfoUtil {
	public static List<MerchantInfoListPageForm> makeMerchantInfoListPageForm(List<MerchantInfo> miList){
		List<MerchantInfoListPageForm> returnList = null;
		if(miList != null && miList.size()>0){
			returnList = new ArrayList<MerchantInfoListPageForm>();
			MerchantInfoListPageForm milpf = null;
			for(MerchantInfo mi:miList){
				milpf = new MerchantInfoListPageForm();
				milpf.setId(mi.getId());
				milpf.setMerchantName(mi.getMerchantName());
				milpf.setDelFlag(DicInit.getSystemCode("base", "del_flag", mi.getDelFlag()));
				milpf.setActiveFlag(Boolean.parseBoolean(milpf.getDelFlag())?"False":"True");				
				milpf.setEnableFlag(Boolean.parseBoolean(milpf.getDelFlag())?"Enable":"Disable");				
				milpf.setCreateTimeStr(DateTimeUtil.formatTimeByFormat(mi.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				milpf.setUpdateTimeStr(DateTimeUtil.formatTimeByFormat(mi.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				milpf.setDisplayName(mi.getDisplayName());
				returnList.add(milpf);
			}
		}
		return returnList;
	}
	
	public static List<MerchantOperatorListPageForm> makeMerchantOperatorListPageForm(List<UserInfo> uiList){
		List<MerchantOperatorListPageForm> returnList = null;
		if(uiList != null && uiList.size()>0){
			returnList = new ArrayList<MerchantOperatorListPageForm>();
			MerchantOperatorListPageForm molpf = null;
			for(UserInfo ui:uiList){
				molpf = new MerchantOperatorListPageForm();
				molpf.setId(ui.getId());
				molpf.setMerchantName(ui.getMerchant()==null?"":ui.getMerchant().getMerchantName());
				molpf.setDelFlag(DicInit.getSystemCode("base", "del_flag", ui.getDelFlag()));
				molpf.setActiveFlag(Boolean.parseBoolean(molpf.getDelFlag())?"False":"True");				
				molpf.setEnableFlag(Boolean.parseBoolean(molpf.getDelFlag())?"Enable":"Disable");
				molpf.setOperatorName(ui.getUserName());
				molpf.setRoleName(OperatorRoleUtil.getInstance().getRoleNameFromId(ui.getRoleId()));
				returnList.add(molpf);
			}
		}
		return returnList;
	}
	
	public static UpdateMerchantInfoPageForm makeUpdateMerchantInfoPageForm(MerchantInfo mi, MerchantBankAccount mba){
		UpdateMerchantInfoPageForm umipf = new UpdateMerchantInfoPageForm();
		umipf.setId(mi.getId());
		umipf.setMerchantName(mi.getMerchantName());
		umipf.setEmail(mi.getEmail());
		umipf.setDisplayName(mi.getDisplayName());
		umipf.setIndustry(mi.getIndustry());
		umipf.setFeeRate(mi.getFeeRate().toString());
		if(mba != null){
			umipf.setAccountName(mba.getAccountName());
			umipf.setAccountNumber(mba.getAccountNumber());
			umipf.setBankType(mba.getBankType());
			umipf.setCurrency(mba.getCurrency());
			umipf.setBankBranchName(mba.getBankBranchName());
		}
		return umipf;
	}
	
	public static String getSecondaryMerchantIndustry(MerchantInfo mi){
		return mi.getIndustry();
	}
	
	public static List<MerchantGroupListPageForm> makeMerchantGroupListPageForm(List<MerchantGroup> mgList){
		List<MerchantGroupListPageForm> returnList = null;
		if(mgList != null && mgList.size()>0){
			returnList = new ArrayList<MerchantGroupListPageForm>();
			MerchantGroupListPageForm mglpf = null;
			for(MerchantGroup mg:mgList){
				mglpf = new MerchantGroupListPageForm();
				mglpf.setId(mg.getId());
				mglpf.setMerchantGroupName(mg.getGroupName());
				mglpf.setCreateTimeStr(DateTimeUtil.formatTimeByFormat(mg.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				mglpf.setUpdateTimeStr(DateTimeUtil.formatTimeByFormat(mg.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				returnList.add(mglpf);
			}
		}
		return returnList;
	}
	
	public static String makeMerchantBracketsStringFromMerchantList(List<MerchantInfo> merchantList){
		StringBuilder sb = new StringBuilder();
			sb.append("(");
			if(merchantList != null && merchantList.size()>0){
				for(MerchantInfo mi:merchantList){
					sb.append(mi.getId()).append(",");
				}
				sb.deleteCharAt(sb.length()-1);
			}
			if(sb.length()==1){
				// if merchantList is empty, I need put in something to make sure SQL runs correct
				sb.append(0);
			}
			sb.append(")");			
		return sb.toString();
	}
	
	public static void main(String[] args){
		List<MerchantInfo> merchantList = new ArrayList<MerchantInfo>();
		for(int i=1;i<5;i++){
			MerchantInfo mi = new MerchantInfo();
			mi.setId(i);
			merchantList.add(mi);
		}
		System.out.println(makeMerchantBracketsStringFromMerchantList(merchantList));
	}
}